package de.elmar_baumann.jbirthdays.xmlrepo;

import de.elmar_baumann.jbirthdays.api.RepositoryChangedEvent;
import de.elmar_baumann.jbirthdays.util.Bundle;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.bushe.swing.event.annotation.AnnotationProcessor;
import org.bushe.swing.event.annotation.EventSubscriber;

/**
 * @author Elmar Baumann
 */
class XmlFilePersonRepositorySettingsPanel extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;

    XmlFilePersonRepositorySettingsPanel() {
        initComponents();
        postInitComponents();
    }

    private void postInitComponents() {
        textFieldFile.setText(XmlFilePersonRepository.getFilename());
        AnnotationProcessor.process(this);
    }

    private void chooseDatabase() {
        JFileChooser fc = new JFileChooser(new File(XmlFilePersonRepository.getFilename()).getParent());
        fc.setMultiSelectionEnabled(false);
        fc.setDialogTitle(Bundle.getString(XmlFilePersonRepositorySettingsPanel.class, "XmlFilePersonRepositorySettingsPanel.ChooseDatabase.FileChosserTitle"));
        String filterDescription = Bundle.getString(XmlFilePersonRepositorySettingsPanel.class, "XmlFilePersonRepositorySettingsPanel.FileFilterDescription");
        FileNameExtensionFilter filter = new FileNameExtensionFilter(filterDescription, "xml");
        fc.setFileFilter(filter);
        fc.setAcceptAllFileFilterUsed(false);
        if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            XmlFilePersonRepository.INSTANCE.setFile(fc.getSelectedFile());
        }
    }

    private void moveDatabase() {
        JFileChooser fc = new JFileChooser(new File(XmlFilePersonRepository.getFilename()).getParent());
        fc.setMultiSelectionEnabled(false);
        fc.setDialogTitle(Bundle.getString(XmlFilePersonRepositorySettingsPanel.class, "XmlFilePersonRepositorySettingsPanel.MoveDatabase.FileChosserTitle"));
        fc.setMultiSelectionEnabled(false);
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {
                XmlFilePersonRepository.INSTANCE.moveTo(fc.getSelectedFile());
                textFieldFile.setText(XmlFilePersonRepository.getFilename());
            } catch (Throwable t) {
                Logger.getLogger(XmlFilePersonRepositorySettingsPanel.class.getName()).log(Level.SEVERE, null, t);
                String message = Bundle.getString(XmlFilePersonRepositorySettingsPanel.class, "XmlFilePersonRepositorySettingsPanel.MoveDatabase.Error.Message", t.getLocalizedMessage());
                String title = Bundle.getString(XmlFilePersonRepositorySettingsPanel.class, "XmlFilePersonRepositorySettingsPanel.MoveDatabase.Error.Title");
                JOptionPane.showMessageDialog(this, message, title, JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    @EventSubscriber(eventClass = RepositoryChangedEvent.class)
    public void databaseMoved(RepositoryChangedEvent evt) {
        textFieldFile.setText(XmlFilePersonRepository.getFilename());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        panelContent = new javax.swing.JPanel();
        labelFile = new javax.swing.JLabel();
        textFieldFile = new javax.swing.JTextField();
        panelButtons = new javax.swing.JPanel();
        buttonMove = new javax.swing.JButton();
        buttonChooseDatabase = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        panelContent.setLayout(new java.awt.GridBagLayout());

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("de/elmar_baumann/jbirthdays/xmlrepo/Bundle"); // NOI18N
        labelFile.setText(bundle.getString("XmlFilePersonRepositorySettingsPanel.labelFile.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        panelContent.add(labelFile, gridBagConstraints);

        textFieldFile.setEditable(false);
        textFieldFile.setColumns(40);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        panelContent.add(textFieldFile, gridBagConstraints);

        panelButtons.setLayout(new java.awt.GridBagLayout());

        buttonMove.setText(bundle.getString("XmlFilePersonRepositorySettingsPanel.buttonMove.text")); // NOI18N
        buttonMove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonMoveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        panelButtons.add(buttonMove, gridBagConstraints);

        buttonChooseDatabase.setText(bundle.getString("XmlFilePersonRepositorySettingsPanel.buttonChooseDatabase.text")); // NOI18N
        buttonChooseDatabase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonChooseDatabaseActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        panelButtons.add(buttonChooseDatabase, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(7, 0, 0, 0);
        panelContent.add(panelButtons, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        add(panelContent, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonChooseDatabaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonChooseDatabaseActionPerformed
        chooseDatabase();
    }//GEN-LAST:event_buttonChooseDatabaseActionPerformed

    private void buttonMoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonMoveActionPerformed
        moveDatabase();
    }//GEN-LAST:event_buttonMoveActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonChooseDatabase;
    private javax.swing.JButton buttonMove;
    private javax.swing.JLabel labelFile;
    private javax.swing.JPanel panelButtons;
    private javax.swing.JPanel panelContent;
    private javax.swing.JTextField textFieldFile;
    // End of variables declaration//GEN-END:variables

}
